package com.soul.dj.ui.fragment;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v4.widget.SwipeRefreshLayout;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.*;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.soul.dj.R;
import com.soul.dj.ui.activity.OptionsScannerActivity;
import com.soul.dj.util.Const;
import com.soul.dj.util.http.JSONCallBack;
import com.soul.dj.util.http.MediaType;
import com.soul.dj.util.http.json.JsonProcessUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author
 * @ClassNmae UnCheckAreaList
 * @create 2018-09-13 14:47
 **/
public class UnCheckAreaListFragment extends BaseFragment{


    private SwipeRefreshLayout swipe;
    private ListView listView;

    private List<Map<String,Object>> items = new ArrayList<Map<String,Object>>();
    private ArrayAdapter<Map<String,Object>> adapter;

    private int page = 1;
    private boolean pull_up_lock;// 锁定上拉
    private View footerView;

    private int lastVisibleItemPosition = 0;// 标记上次滑动位置，初始化默认为0
    private boolean scrollFlag = false;// 标记是否滑动

    private String checkkind;
    private EditText keyEdit;

    public UnCheckAreaListFragment()
    {

    }

    public static UnCheckAreaListFragment newInstance(String type)
    {
        Bundle args = new Bundle();
        args.putString("checkkind" , type);
        UnCheckAreaListFragment fragment = new UnCheckAreaListFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        activity = getActivity();
        checkkind = getArguments().getString("checkkind");
        View v = inflater.inflate(R.layout.uncheck_area_list_fragment, container, false);

        v.findViewById(R.id.searchBtn).setOnClickListener(this);

        keyEdit =v.findViewById(R.id.key);
        swipe =  v.findViewById(R.id.swipe);
        listView = v.findViewById(R.id.listView);

        swipe.setColorSchemeResources(android.R.color.holo_blue_bright,
                android.R.color.holo_green_light,
                android.R.color.holo_orange_light,
                android.R.color.holo_red_light);
        swipe.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                initLoadData();
            }
        });

        adapter = new ArrayAdapter<Map<String,Object>>(activity, android.R.layout.simple_list_item_1, items){

            @Override
            public View getView(final int position, View convertView, ViewGroup parent) {

                View	view = convertView;
                final ViewHolder  holder;

                final Map<String,Object> item = items.get(position);
                if(view == null){
                    view = activity.getLayoutInflater().inflate(R.layout.uncheck_area_list_item, null);
                    holder = new ViewHolder();
                    holder.title = view.findViewById(R.id.title);
                    holder.area = view.findViewById(R.id.area);
                    holder.cnt =  view.findViewById(R.id.cnt);
                    holder.scannerBtn = view.findViewById(R.id.scannerBtn);

                    view.setTag(holder);
                }else{
                    holder = (ViewHolder) view.getTag();
                }
                view.setOnClickListener(new View.OnClickListener() {

                    @Override
                    public void onClick(View v) {
//
//                        Intent intent = new Intent(activity, UnCheckPartListActivity.class);
//                        try {
//                            intent.putExtra("areaMap", JsonProcessUtil.beanToJson(item));
//                        } catch (JsonProcessingException e) {
//                            // TODO Auto-generated catch block
//                            e.printStackTrace();
//                        }
//
//                        startActivity(intent);
//                    }
//                });
//
//                holder.scannerBtn.setOnClickListener(new View.OnClickListener() {
//
//                    @Override
//                    public void onClick(View v) {

                        if (ContextCompat.checkSelfPermission(activity, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
                            ActivityCompat.requestPermissions(activity, new String[]{Manifest.permission.CAMERA}, 1);
                        } else {
                            Intent intent = new Intent(activity, OptionsScannerActivity.class);
                            try {
                                intent.putExtra("areaMap", JsonProcessUtil.beanToJson(item));
                            } catch (JsonProcessingException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                            }

                            startActivity(intent);
                        }
                    }
                });
//                "MAINNAME": "测试卸船机",
//                        "CHECKAREAID": "A001",
//                        "CHECKID": "E006DAY180818",
//                        "UNCHECKNUM": 131,
//                        "CHECKAREANAME": "点检区域I",
//                        "MAINID": "E006"
                holder.title.setText(item.get("MAINNAME").toString());
                holder.area.setText(item.get("CHECKAREANAME").toString());
                holder.cnt.setText(item.get("UNCHECKNUM").toString()+"项");

                return view;
            }
        };

        listView.setAdapter(adapter);

        return v;
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()){
            case R.id.searchBtn:
                initLoadData();
                break;
        }
    }


    private void initLoadData() {

        page = 1;
        setProgressEnabled(false);
        Map<String,Object> paramsMap = new HashMap<>();
        paramsMap.put("checkkind", checkkind);
        paramsMap.put("mainid", keyEdit.getText().toString());
//        sendHttp(Const.REQUEST_URL_GETUNCHECKAREALIST,MediaType.JSON,paramsMap,new JSONCallBack<List>(this,"lodeData", List.class));
        swipe.setRefreshing(true);

    }



    @Override
    public void onAjaxOK(Object result, String tag) {
        super.onAjaxOK(result, tag);

        if(TextUtils.equals("lodeData", tag)){
            swipe.setRefreshing(false);
            List list = (List) result;

            items.clear();
            if (list != null && list.size() > 0) {
                for (Object object : list) {
                    try {
                        String tmp = JsonProcessUtil.beanToJson(object);
                        Map<String,Object> good = JsonProcessUtil.jsonToBean2(tmp , Map.class);
                        items.add(good);
                    } catch (Exception e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }

                page++;
            }

            adapter.notifyDataSetChanged();
        }
    }
    @Override
    public void onResume() {
        // TODO Auto-generated method stub
        super.onResume();
        initLoadData();
    }


   private class ViewHolder{

        private TextView	title;
        private TextView	area;
        private TextView	cnt;
        private LinearLayout scannerBtn;
    }

}
